import sys
import os
import re

def ConfigureHttpd(configPath, serverIP, serverPort, htdocsPath):
    if not os.path.isfile(configPath):
        print("Invalid Httpd Configure file: " + configPath)
        return

    htdocsRootChanged = False
    
    modifiedConfLines = ""
    with open(configPath, "r") as conf:
        confLines = conf.readlines()
        for line in confLines:
            if re.match(".*LoadModule socache_shmcb_module modules/mod_socache_shmcb.so.*", line):
                modifiedConfLines += "LoadModule socache_shmcb_module modules/mod_socache_shmcb.so\n"
            elif re.match(".*LoadModule ssl_module modules/mod_ssl.so.*", line):
                modifiedConfLines += "LoadModule ssl_module modules/mod_ssl.so\n"
                # Also introduce new module
                modifiedConfLines += "LoadModule rewrite_module modules/mod_rewrite.so\n"
            elif re.match("^ServerName .*", line):
                modifiedConfLines += "ServerName " + serverIP + ":" + serverPort + "\n"
            elif re.match("^#*ServerName .*80*", line):
                modifiedConfLines += "ServerName " + serverIP + ":" + serverPort + "\n"
            elif re.match("^DocumentRoot .*", line):
                modifiedConfLines += "DocumentRoot \"" + htdocsPath + "\"\n"
                htdocsRootChanged = True
            elif re.match("^\<Directory .*", line) and htdocsRootChanged:
                modifiedConfLines += "<Directory \"" + htdocsPath + "\">\n"
            elif re.match("^    AllowOverride .*", line) and htdocsRootChanged:
                modifiedConfLines += "    AllowOverride All\n"
                htdocsRootChanged = False
            elif re.match("\<IfModule mime_module\>.*", line):
                modifiedConfLines += line
                modifiedConfLines += "    AddType application/x-httpd-php .php .phtml .php3 .inc\n"
                modifiedConfLines += "    AddType application/x-httpd-source .phps\n"
            elif re.match(".*Include conf/extra/httpd-vhosts.conf.*", line):
                modifiedConfLines += "Include conf/extra/httpd-vhosts.conf\n"
            elif re.match(".*Include conf/extra/httpd-ssl.conf.*", line):
                modifiedConfLines += "Include conf/extra/httpd-ssl.conf\n"
            else:
                modifiedConfLines += line
    modifiedConfLines += "<FilesMatch \.html$>\n"
    modifiedConfLines += "    SetHandler application/x-httpd-php\n"
    modifiedConfLines += "</FilesMatch>\n"
                
    with open(configPath + ".modify.tmp", "w") as modifiedConf:
        modifiedConf.write(modifiedConfLines)

def ConfigureVHost(configPath, serverIP):
    if not os.path.isfile(configPath):
        print("Invalid VHost Configure file: " + configPath)
        return

    modifiedConfLines = ""
    with open(configPath, "r") as conf:
        confLines = conf.readlines()
        for line in confLines:
            if re.match("^#.*", line):
                modifiedConfLines += line
    modifiedConfLines += "<VirtualHost *:80>\n"
    modifiedConfLines += "    ServerAdmin webmaster@dummy-host.example.com\n"
    modifiedConfLines += "    ServerName " + serverIP + "\n"
    modifiedConfLines += "    Redirect permanent  / https://" + serverIP + "\n"
    modifiedConfLines += "</VirtualHost>\n"

    with open(configPath + ".modify.tmp", "w") as modifiedConf:
        modifiedConf.write(modifiedConfLines)

def ConfigureSSL(configPath, serverIP, htdocsPath):
    if not os.path.isfile(configPath):
        print("Invalid SSL Configure file: " + configPath)
        return

    modifiedConfLines = ""
    with open(configPath, "r") as conf:
        confLines = conf.readlines()
        for line in confLines:
            if re.match("^ServerName.*", line):
                modifiedConfLines += "ServerName " + serverIP + ":443\n"
            elif re.match("^DocumentRoot .*", line):
                modifiedConfLines += "DocumentRoot \"" + htdocsPath + "\"\n"
            else:
                modifiedConfLines += line

    with open(configPath + ".modify.tmp", "w") as modifiedConf:
        modifiedConf.write(modifiedConfLines)
        
if __name__ == "__main__":
    argLen = len(sys.argv)
    if argLen == 7:
        ConfigureHttpd(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
        ConfigureVHost(sys.argv[5], sys.argv[2])
        ConfigureSSL(sys.argv[6], sys.argv[2], sys.argv[4])
    else:
        print("Wrong parameter for ConfigureHpptd.py, usage:")
        print("python ConfigureHpptd.py, httpd.conf_PATH SERVER_IP SERVER_PORT htdocs_DIR vhostConf_PATH sslConf_PATH")
